﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Engine.GUI
{
    public class GUIDefaultButton:GUIBase
    {
        SpriteBatch sp;
        SpriteFont font;
        Skin _skin;
        Texture2D texturePres;
        Texture2D textureUnPres;
         
        public int x, y, Height, Width;
        int BHeight, BWidth;
        Color pres = Color.Gray;
        Color UnPres = Color.Goldenrod;
        Color textColor = Color.Black;
        public string lable;
        Vector2 measure;
        MouseState last;
        bool isPress = false;
        bool btFocus = false;
        public GUIDefaultButton(NOWAEngine _game,string name):base(_game,name)
        {
            lable = name;
            BHeight = 23;
            BWidth = 140;
            Height = 40;
            Width = 150;

            x = 10;
            y = 50;

        }
        public override void Initialize()
        {
            last = Mouse.GetState();
            base.Initialize();
        }    
        public override void LoadContent()
        {
            sp = new SpriteBatch(Game.GraphicsDevice);
            font = Game.Content.Load<SpriteFont>("default");
            Texture2D gloB = Game.Content.Load<Texture2D>("Skin");
            
            texturePres = new Texture2D(Game.GraphicsDevice,BWidth, BHeight);
            textureUnPres = new Texture2D(Game.GraphicsDevice, BWidth, BHeight);
            measure= font.MeasureString(lable);
            Color[] t = new Color[BWidth* BHeight];
            //for (int i = 0; i < t.Length; i++)
            //{
            //    t[i] = pres;
            //}
            gloB.GetData<Color>(0, new Rectangle(0, 0, BWidth , BHeight), t, 0, BWidth * BHeight);
            textureUnPres.SetData<Color>(t);
            gloB.GetData<Color>(0, new Rectangle(0, 24, BWidth , BHeight), t, 0, BWidth * BHeight);
            texturePres.SetData<Color>(t);
            _rect = new Rectangle(x, y, Width ,Height);
            base.LoadContent();
        }
        public override void Update(GameTime gameTime)
        {
            MouseState ms = ((Game as NOWAEngine).Manager.GetComponent("InputManager") as Components.InputManager).MouseState;
            if (ms.LeftButton!=last.LeftButton && ms.LeftButton==ButtonState.Pressed)
            {
                if (_rect.Contains(ms.X,ms.Y))
                {
                     isPress = true;
                last = ms;
                }
               
            }
            if (ms.LeftButton != last.LeftButton && ms.LeftButton == ButtonState.Released)
            {
                
                isPress = false;
                last = ms;
            }
            if (_rect.Contains(ms.X, ms.Y))
            {
                btFocus = true;
            }
            else
            {
                btFocus = false;
            }
            
            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            sp.Begin();
            if (isPress)
            {
                sp.Draw(texturePres, rect, Color.White);
            }
            else
            {
                if (btFocus)
                {
                    sp.Draw(texturePres, rect, Color.Gray);
                }
                else
                {
                    sp.Draw(textureUnPres, rect, Color.White);
                }
            }
            Vector2 pos = new Vector2(x + rect.Width / 2 - measure.X / 2, y + rect.Height / 2 - measure.Y / 2);            
            sp.DrawString(font, lable, pos, textColor);
            sp.End();
            base.Draw(gameTime);
        }
        Rectangle _rect;
        public Rectangle rect { get { return _rect; } }
    }
}
